import { _decorator, CCInteger, Label, Sprite, UITransform } from 'cc';
import { VirtualItem } from '../VirtualItem';
const { ccclass, property } = _decorator;

@ccclass('ChatIItem')
export class ChatIItem extends VirtualItem {
    @property(Label)
    text: Label;

    @property(Sprite)
    emoji: Sprite;

    @property(UITransform)
    bubble: UITransform;

    @property(CCInteger)
    paddingLeft = 10;

    @property(CCInteger)
    paddingRight = 10;

    @property(CCInteger)
    paddingTop = 10;

    @property(CCInteger)
    paddingBottom = 10;

    maxWidth = 300;

    setData(index: number, data: { self: boolean, emoji: string, text: string }) {
        if (data.text) {
            this.text.overflow = Label.Overflow.NONE;
            this.text.string = data.text;
            if (data.self) {
                this.text.horizontalAlign = Label.HorizontalAlign.RIGHT;
            }
            this.text.updateRenderData();
            if (this.text.node.getComponent(UITransform).width > this.maxWidth) {
                this.text.horizontalAlign = Label.HorizontalAlign.LEFT;
                this.text.overflow = Label.Overflow.RESIZE_HEIGHT;
                this.text.node.getComponent(UITransform).width = this.maxWidth;
                this.text.updateRenderData();
            }
            this.bubble.width = this.text.node.getComponent(UITransform).width + this.paddingLeft + this.paddingRight;
            this.bubble.height = this.text.node.getComponent(UITransform).height + this.paddingTop + this.paddingBottom;
            if (data.self) {
                this.text.node.x = -this.paddingRight;
            } else {
                this.text.node.x = this.paddingLeft;
            }
            this.text.node.y = -this.paddingTop;
        }
    }
}


